package com.zuokun.config;


import com.zuokun.annotation.Controller;
import com.zuokun.annotation.Protocol;
import com.zuokun.controller.LoginController;
import com.zuokun.dispatcher.ProtocolDispatcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Map;

@Configuration
@Slf4j
public class ProtocolConfig {

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        // 获取所有带 @Controller 注解的 Bean
        Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(Controller.class);
        for (Object controller : controllers.values()) {
            ProtocolDispatcher.registerControllers(controller);
        }
        // 获取所有带 @Protocol 注解的 Bean
        Map<String, Object> protocols = applicationContext.getBeansWithAnnotation(Protocol.class);
        ProtocolDispatcher.registerProtocols(protocols);
        log.info("协议处理器已注册！");
    }
}